<template>
  <div class="container">
       <el-table
        :data="tableData"
        style="width: 100%"
        v-loading="listLoading"
        element-loading-text="正在快马加鞭中~"
        >
        <el-table-column

          prop="id"
          label="编号"
          width="180"
          >
        </el-table-column>
        <el-table-column
          align="center"
          prop="questionType"
          label="题型"
          width="180"
          >
        </el-table-column>
        <el-table-column
        prop="questionIDs"
        label="题目编号"
        >
        <!-- 作用域插槽 -->
          <template #default="{row}">
            <div  v-for="(item,index) in row.questionIDs" :key="index">
              <el-button type="text" class="questionID" @click="showDialog(item)">
                {{item.number}}
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column
        prop="addTime"
        label="录入时间"
        >
        </el-table-column>
        <el-table-column
        align="center"
        prop="totalSeconds"
        label="答题时间"
        >
        </el-table-column>
        <el-table-column
        align="center"
        prop="accuracyRate"
        label="正确率(%)"
        >
        </el-table-column>
        <el-table-column
         align="center"
        prop="userName"
        label="录入人"
        >
        </el-table-column>
        <!-- 删除操作 -->
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button size="small" @click="delQuestion(row)" type="danger" plain circle icon="el-icon-delete">
            </el-button>
          </template>
        </el-table-column>
    </el-table>
    <!-- 底部分页 -->
    <el-row type="flex" justify="end">
      <PageTool
        :paginationPage="page.page"
        :paginationPagesize="page.pagesize"
        :total="page.total"
        @pageChange="handleCurrentChange"
        @pageSizeChange="handleSizeChange"
      ></PageTool>
    </el-row>
    <!-- 预览弹层 -->
    <DetailDialog ref="detailData" :isShowDialog.sync="isShowDialog"/>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions'
import PageTool from '@/module-manage/components/page-tool'
import { questionType } from '@/api/hmmm/constants'
import DetailDialog from './detail-dialog.vue'
export default {
  components: {
    PageTool,
    DetailDialog
  },
  data () {
    return {
      tableData: [], // 表格数据
      page: { // 分页
        page: 1,
        pagesize: 20,
        total: '',
        keyword: '' // 搜索关键字
      },
      listLoading: false, // 等待
      isShowDialog: false // 预览弹层
    }
  },
  created () {
    this.randoms()
  },
  methods: {
    // 获取数据
    async randoms () {
      this.listLoading = true
      const { data, data: { items } } = await randoms(this.page)
      // table数据
      // 进行数据替换
      items.map((info) => {
        questionType.forEach(obj => {
          // 判断
          if (obj.value === Number(info.questionType)) {
            // 重新赋值
            info.questionType = obj.label
          }
        })
        // 返回info对象
        return info
      })
      this.tableData = items // 赋值给表格数据
      // console.log(this.tableData)
      this.page.total = data.counts // 分页显示总条数
      // 子传父 总数，用于提示信息显示
      this.$emit('checkVal', data.counts)
      this.listLoading = false
    },
    // 当前页改变
    handleCurrentChange (pageNum) {
      this.page.page = pageNum
      this.randoms()
    },
    // 每页显示条数发生改变
    handleSizeChange (pageSize) {
      // 默认变化后从第一页开始
      this.page.page = 1
      this.page.pagesize = pageSize
      this.randoms()
    },
    // 删除数据
    delQuestion (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(_ => {
        return removeRandoms(row)
      }).then(_ => {
        this.$message.success('删除成功')
        // 重新获取数据
        this.randoms()
      })
    },
    // 题目详情弹层
    showDialog (item) {
      this.isShowDialog = true
      // 调用子组件的方法
      this.$refs.detailData.detailInfo(item.id)
    },
    // 搜索
    search (input) {
      this.page.keyword = input
      this.randoms()
    }
  }
}
</script>

<style scoped>
.questionID {
  /* display: block; */
  color: skyblue;
}
.questionID:hover {
  color: blue;
}
</style>
